<?php

class Box extends CWidget{


    public $title = 'Title';

    public $controls = '';

    public $search = null;

    public function init(){

        $icon = '';
        // icon definition
        if(substr($this->title, 0, 1) == '!'){
            $this->title = explode(' ', $this->title, 2);
            $icon = substr($this->title[0], 1);

            $icon = '<i class="icon-'.$icon. '"></i> ';

            $this->title = $this->title[1];
        }

        ?>
        <div class="box gradient">
           <div class="title">
            <div class="row-fluid">
                <div class="span6">
                    <h4>
                    <span><?php echo $icon . $this->title; ?></span>
                    </h4>
                </div>
                <!-- End .span6 -->
                <div class="span6 to_hide right_offset">

                    <?php echo $this->renderSearch(); ?>

                    <div class="btn-toolbar">
                        <div class="options_arrow pull-right">



                        <?php echo $this->controls; ?>

                       <!--

                       <div class="dropdown pull-right">
                        <a class="dropdown-toggle " id="dLabel" role="button" data-toggle="dropdown" data-target="#" href="/page.html">
                            <i class=" icon-caret-down"></i>
                        </a>
                            <ul class="dropdown-menu " role="menu" aria-labelledby="dLabel">
                            <li><a href="#">Today</a></li>
                            <li><a href="#">Yesterday</a></li>
                            <li><a href="#">Last 7 Days</a></li>
                            <li><a href="#">Last 30 Days</a></li>
                            </ul>
                        </div>
                        -->
                      </div>
                   </div>
                </div>
                        <!-- End .span6 -->
             </div>
                        <!-- End .row-fluid -->
           </div>
                        <!-- End .title -->
        <div class="content">
                       <?php

    }

    public function run(){

      return "  </div>
          </div>
        </div>";

    }

    public function renderSearch(){
        $o = '';

        if(isset($this->search)){

            ob_start();
            $form=$this->beginWidget('bootstrap.widgets.BootActiveForm',array(
                    'id'=>'list-search-form',
                    'enableAjaxValidation'=>false,
                    'method'=>'get',
                    'type'=>'horizontal',
                    'htmlOptions'=>array(
                            'enctype'=>'multipart/form-data',
                             'style'=>'height: 24px; padding-top: 5px;')));

                    if(is_array($this->search)){
                      echo '<div class="row-fluid">';
                      echo '<div class="dataTables_filter">';

                      $model = $this->search[0];
                      $field = $this->search[1];
                      $htmlOptions = array('class'=>' pull-right small', 'placeholder'=>'Keresés...');

                      CHtml::resolveNameID($model, $field, $htmlOptions);

                      echo CHtml::activeTextField($model, $field, $htmlOptions);
                      echo '</div>';
                      echo '</div>';

                      // set focus to search
                      if(strlen($model->$field) > 0){
                          registerScriptFile(baseUrl() .'/js/jquery.cursorEnd.js');

                          registerScript('let-focus-search', <<<SCRIPT
                          jQuery('#{$htmlOptions['id']}').cursorEnd();
SCRIPT
, CClientScript::POS_READY);
                      }

                    }

            $this->endWidget();

            $o .= ob_get_clean();

        }

        return $o;

     }

}